package com.traffic.job;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.traffic.constant.Const;
import com.traffic.entity.*;
import com.traffic.mapper.LeaderboardpreMapper;
import com.traffic.service.*;
import com.traffic.util.CalculateUtil;
import com.traffic.util.RUtil;
import lombok.extern.slf4j.Slf4j;
import org.rosuda.REngine.REXPMismatchException;
import org.rosuda.REngine.REngineException;
import org.rosuda.REngine.Rserve.RserveException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.traffic.util.RankUtil;

/**
 * Created by IntelliJ IDEA.
 * User: KingRainGrey
 * Date: 2020/9/14
 * 定时处理每天产生的验证码数据 注意调整linux的时区
 */

@Component
@Slf4j
public class SmsJob {
    @Autowired
    private SmsService smsService;

    @Autowired
    RankUtil rankUtil;

    @Autowired
    RUtil rUtil;

    @Autowired
    LeaderboardService leaderboardService;

    @Autowired
    LeaderboardpreService leaderboardpreService;

    @Autowired
    RankService rankService;

    @Autowired
    CalculateUtil calculateUtil;

    @Autowired
    TrajectoryService trajectoryService;

    // second(秒), minute（分）, hour（时）, day of month（日）, month（月）, day of week（周几）
    @Scheduled(cron = "0 59 23 ? * *") // 每天的23:59清空
    public void sms() throws RserveException, REXPMismatchException, REngineException {
        log.info("【SMS任务启动】:{}", DateUtil.formatDate(new Date()));
        QueryWrapper<Sms> wrapper = new QueryWrapper<>();
        wrapper.eq("status", Const.SMS_TODAY);
        List<Sms> list = smsService.list(wrapper);
        list.forEach(sms -> {
            sms.setStatus(Const.SMS_OLD);
        });
        if (ObjectUtil.isNotNull(list) && list.size() > 0) {
            smsService.updateBatchById(list);
        }
        log.info("【SMS任务结束】:{}", DateUtil.formatDate(new Date()));

    }

    @Scheduled(fixedRate=5000*12*30) // 每天的23:59清空
    public void updateRankList() {

        log.info("【等级开始更新:】:{}", DateUtil.formatDate(new Date()));
        Double mileage=0.0;
        int level = 0;
        List<Rank> list = rankService.list();
        for (Rank r:list) {
            mileage = r.getHisMileage();
            level = calculateUtil.CalculateLevel(mileage);
            r.setLevel(level);

            rankService.updateById(r);
        }


        log.info("【排行榜开始更新:】:{}", DateUtil.formatDate(new Date()));
        List<Leaderboardpre> lbp= new ArrayList<>();
        List<Leaderboard> lb = leaderboardService.list();
        for (Leaderboard l:lb) {
            Leaderboardpre temp = new Leaderboard();
            temp = l;
            lbp.add(temp);
        }

        leaderboardpreService.updateBatchById(lbp);



        //排行榜更新
        String types[] = new String[]{"level","score","stationarity","all_time",
                "morning_time","afternoon_time","evening_time",
                "his_vel","his_acc","his_mileage","max_vel",
                "max_acc","max_n_acc"};

        //  String types[] = new String[]{"his_mileage"};
        for(String type : types){
            rankUtil.sortRank(type);
        }

        //逻辑回归type1 2参数自动更新

//        rUtil.R_MaxLik(1);
//        rUtil.R_MaxLik(2);
        log.info("【排行榜更新结束】:{}", DateUtil.formatDate(new Date()));
    }

    @Scheduled(cron = "0 50 23 ? * *") // 每天的23:59清空
    public void clearTrajectory() {

        log.info("【开始清理1个月以上轨迹记录:】:{}", DateUtil.formatDate(new Date()));

        UpdateWrapper<Trajectory> wrapper = new UpdateWrapper<>();

        trajectoryService.update(wrapper.set("points","").last("WHERE TO_DAYS( NOW( ) ) - TO_DAYS(start_time) >= 30"));


    }



}
